(function (global, factory) {
	typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
	typeof define === 'function' && define.amd ? define(factory) :
  // here name is your name
	(global.paging = factory());
}(this, (function () { 
    'use strict';
    if (typeof jQuery === undefined) {
        throw new Error('util needs jQuery')
    }

    var $=jQuery;

    // var page=paging();
    // page.init();

    return function(){
        

        return {
            page:0,
            total:0,
            dom:$('body'),
            cb:function(){
                console.log($(this).data('page'));
            },
            limit:6,
            totalPage:0,

            /**
             * @param limit how many on one page
             * @param dom your jquery dom,will fill html
             * @param page the page you want to show
             * @param total how many pages
             * @param cb when you click to run a callback
             */
            
            init:function(page,total,dom,cb,limit){

                this.page=parseInt(page) || this.page;
                if (typeof total!=="undefined"){
                    this.total=parseInt(total)
                }
                this.dom=dom || this.dom;
                this.cb=cb || this.cb;
                this.limit=parseInt(limit) || this.limit;
                this.totalPage=Math.ceil(this.total / this.limit);

                this.dom.find('a').off();
                this.dom.empty();

                if (parseInt(this.totalPage)===0){
                    this.dom.hide();
                    return;
                }else{
                    this.dom.show();
                }

                var countHtml='<span class="count">'+this.page+'/'+this.totalPage+'</span> ';
                var prevHtml=(parseInt(this.page)>1)?'<a href="javascript:;" class="prev" data-page="'+parseInt(this.page-1)+'">上一页</a> ':'';
                var nextHtml=(parseInt(this.page)<parseInt(this.totalPage)?'<a href="javascript:;" class="next" data-page="'+parseInt(this.page+1)+'">下一页</a> ':'')

                var pageHtml='<span class="page">';
                
                // to the page
                if (this.page-1<3){
                    for (var i=1;i<=this.page;i++){
                        pageHtml+='<a href="javascript:;" data-page="'+i+'" '+((i===parseInt(this.page))?'class="active"':'')+'>'+i+'</a> ';
                    }
                }else{
                    pageHtml+='<a href="javascript:;" data-page="1">1</a> <span>...</span> ';
                    pageHtml+='<a href="javascript:;" data-page='+parseInt(this.page-1)+'>'+parseInt(this.page-1)+'</a> ';
                    pageHtml+='<a href="javascript:;" class="active" data-page='+this.page+'>'+this.page+'</a> ';
                }

                //page+1
                if (this.totalPage-this.page<3)
                {
                    for (var j=this.page+1;j<=this.totalPage;j++)
                    {
                        pageHtml+='<a href="javascript:;" data-page="'+j+'">'+j+'</a> ';
                    }
                }else{
                    pageHtml+='<a href="javascript:;" data-page="'+parseInt(this.page+1)+'">'+parseInt(this.page+1)+'</a>'
                    pageHtml+=' <span>...</span> '
                    pageHtml+='<a href="javascript:;" data-page="'+parseInt(this.totalPage)+'">'+parseInt(this.totalPage)+'</a>';
                }
                
                pageHtml+='</span>';

                this.dom.html(countHtml+prevHtml+pageHtml+nextHtml);

                //bind activity
                this.dom.find('a').on('click',this.cb);
            }
        }
    }


})))